package com.brotherly.framework.service;

import android.content.Context;
import android.util.Log;

import com.brotherly.framework.storage.SimpleStorage;
import com.brotherly.framework.storage.SimpleStorageService;

public class WeatherSetting
{
    public String[] settedCitys =
    {
            "当前城市"
    }; // 用户设置城市
    public String settedCitySelected = "当前城市"; // 用户选择城市
    public boolean autoUpdate = false; // 自动刷新
    public int updateperiod = 30 * 60 * 1000; // 单位毫秒

    /**
     * 从存储中取出设置
     * 
     * @param context
     * @param storageService
     */
    public void update(Context context, SimpleStorageService storageService)
    {
        if (storageService == null)
            return;

        try
        {
            SimpleStorage storage = storageService.openStorage("autoUpdate");

            /*
             * storage name: autoUpdate; key: add_city 说明:city列表 key:
             * updateState 说明:是否更新，值on/off key: time 说明:刷新时间，单位分钟 key:
             * select_city 说明:选择城市 key: locate_city 当前定位到的城市
             */

            if (storage != null)
            {
                String cityList = storage.get("add_city");
                String citySelect = storage.get("select_city");
                String updateState = storage.get("updateState");
                String periodDescribe = storage.get("time");

                Log.i("AppCommService", "citys=" + cityList + "  select=" + citySelect + "   state=" + updateState
                        + "   period=" + periodDescribe);

                if (cityList != null)
                {
                    settedCitys = cityList.split("\\|");
                } else
                {
                    settedCitys = null;
                }

                settedCitySelected = citySelect;
                if ((settedCitySelected == null || settedCitySelected.length() <= 0) && settedCitys != null
                        && settedCitys.length > 0)
                {
                    settedCitySelected = settedCitys[0];

                    /*
                     * try{ storage.put("select_city", settedCitySelected);
                     * storage.commit(); }catch (Exception e) {
                     * e.printStackTrace(); }
                     */
                }

                if (updateState != null)
                {
                    if (updateState.equals("on"))
                        autoUpdate = true;
                    else
                        autoUpdate = false;
                }

                if (periodDescribe != null && periodDescribe.length() > 0)
                {
                    int minute = Integer.parseInt(periodDescribe);
                    updateperiod = minute * 60 * 1000;
                }
            }
        }
        catch (Exception e)
        {}
    }

    /**
     * 设置当前城市
     * 
     * @param context
     * @param storageService
     * @param city
     */
    public void setCity(Context context, SimpleStorageService storageService, String city)
    {
        try
        {
            if (storageService != null)
            {
                SimpleStorage storage = storageService.openStorage("autoUpdate");

                if (storage != null)
                {
                    storage.put("select_city", city);
                    storage.commit();
                }

                settedCitySelected = city;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 设置定位到的城市,用于显示详情
     * 
     * @param context
     * @param storageService
     * @param city
     */
    public void setLocateCity(Context context, SimpleStorageService storageService, String city)
    {
        try
        {
            if (storageService != null)
            {
                SimpleStorage storage = storageService.openStorage("autoUpdate");

                if (storage != null)
                {
                    storage.put("locate_city", city);
                    storage.commit();
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void addCity(Context context, SimpleStorageService storageService, String city)
    {
        settedCitySelected = city;

        String[] newCitys = new String[settedCitys.length + 1];
        newCitys[0] = city;
        for (int k = 0; k < settedCitys.length; k++)
        {
            newCitys[k + 1] = settedCitys[k];
        }
        settedCitys = newCitys;

        Save(context, storageService);
    }

    public boolean deleteCity(Context context, SimpleStorageService storageService, String city)
    {
        boolean isCurrent = false;

        if (settedCitySelected.equals(city))
        {
            settedCitySelected = "当前城市";

            isCurrent = true;
        }

        int igone = 0;

        String[] newCitys = new String[settedCitys.length - 1];
        for (int k = 0; k < settedCitys.length; k++)
        {
            if (settedCitys[k].equals("当前城市"))
                igone = 1;

            newCitys[k - igone] = settedCitys[k];
        }

        settedCitys = newCitys;

        Save(context, storageService);

        return isCurrent;
    }

    public void Save(Context context, SimpleStorageService storageService)
    {
        try
        {
            if (storageService != null)
            {
                SimpleStorage storage = storageService.openStorage("autoUpdate");

                if (storage != null)
                {
                    storage.put("select_city", settedCitySelected);

                    StringBuffer citys = new StringBuffer();
                    for (String city : settedCitys)
                    {
                        citys.append(city);
                        citys.append("|");
                    }
                    storage.put("add_city", citys.toString());

                    storage.put("updateState", autoUpdate ? "on" : "off");

                    storage.put("time", String.valueOf(updateperiod / 60000));

                    storage.commit();
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
